/*
Copyright (c) 2010 Neil Wood

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package envisage
{
	import flash.display.DisplayObject;
	import flash.display.Stage;
	import flash.geom.Point;
	
	public class Notation
	{
		private static var t0Array:Array = new Array();
		public static function TabsToCols(str:String, colSepSpaces:int = 0):String
		{
			var i:int;
			var len:int;
			var col:int;
			var colLen:int
			
			len = str.length;
			
			col = 0;
			colLen = 0;
			for(i = 0 ; i < len ; i++)
			{
				if( str.charAt(i) == "\t" || str.charAt(i) == "\n" ) 
				{
					
					if( colLen > int(t0Array[col]) ) t0Array[col] = colLen;
					
					colLen = 0;
					col++;
					if( str.charAt(i) == "\n" ) col = 0;
				}
				else colLen++;
			}
			
			var output:String = "";
			
			col = 0;
			colLen = 0;
			for(i = 0 ; i < len ; i++)
			{
				if( str.charAt(i) == "\t" || str.charAt(i) == "\n" )
				{
					var max:int = t0Array[col];
					if( col < (t0Array.length-1) ) max += colSepSpaces;
					for(var g:int = colLen ; g < max ;g++) output += " ";
					
					colLen = 0;
					
					col++;
					if( str.charAt(i) == "\n" ) { col = 0; output += "\n"; }
				}
				else { colLen++; output += str.charAt(i); }
				
				
			}
			
			t0Array.length = 0;
			
			return output;
		}
		
		public static function BytesToReadable(b:uint):String
		{
			var KB:Number = Number(b) / 1024;
			if( KB < 1 ) return b + "B";
			
			var MB:Number = KB / 1024;
			if( MB < 1 ) return Math.round(KB) + "KB";
			
			var GB:Number = MB / 1024;
			if( GB < 1 ) return Math.round(MB) + "MB";
			
			return Math.round(GB) + "GB";
		}
		
		public static function SecondsToReadable(s:Number):String
		{				
			var mins:int = s / 60;
			s -= (mins * 60);
				
			var secs:int = s / 1;
			s -= (secs * 1 );
				
			var tenths:int = s / 0.1;
				
			var strMins:String = "" + mins;
			var strSecs:String = "" + secs;
			var strTenths:String = "" + tenths;
				
			if( strMins.length == 1 ) strMins = "0" + strMins;
			if( strSecs.length == 1 ) strSecs = "0" + strSecs;
				
			return 	strMins + ":" + strSecs + ":" + strTenths;
		}
		
		public static function NumberDecimalPlaces(s:Number, dp:int):String
		{
			var str:String = "" + s;
			var i:int = str.lastIndexOf(".");
			if( i == -1 )
			{
				str += ".";
				i = str.length-1;
			}
			
			str = str.substring(0, i + dp + 1);
			while(str.length < (i+dp + 1) ) str = str + "0";
			return str;
		}
		
		public static function GetObjectsUnderPoint(stage:Stage, x:Number, y:Number, sort:Boolean = false):Array
		{
			var arrD:Array = stage.getObjectsUnderPoint(new Point(x, y));
			
			var i:int;
			var d:DisplayObject;
			
			for(i = 0 ; i < arrD.length ; i++)
			{ 
				d = DisplayObject(arrD[i]).parent;
				
				while(d != null )
				{
					for(var k:int = 0; k < arrD.length ; k++)
						if( arrD[k] == d ) break;
					
					if( k == arrD.length ) arrD.push(d);
							
					d = d.parent;
				}
			}
			
			if( sort == false ) return arrD;
			
			var arrZ:Array = new Array();
			for(i = 0 ; i < arrD.length ; i++)
			{
				d = arrD[i];
				for(arrZ[i] = int(0) ; d.parent != null ; arrZ[i]++) d = d.parent;
			}
				
			arrZ = arrZ.sort(Array.NUMERIC|Array.RETURNINDEXEDARRAY|Array.DESCENDING);
			
			for(i = 0 ; i < arrZ.length ; i++) arrZ[i] = arrD[arrZ[i]];
			
			return arrZ;		
		}
		
		public static function HTMLFromString(text:String, color:uint = 0x000000, fontSize:Number = 12, face:String = "Times", biulrcj:String = "", href:String = ""):String
		{
			var options:String = biulrcj.toLowerCase();
		
			var s:String = "<FONT color='" + RGBStringFromCol(color)
						 + "' size='" + fontSize.toString() + "' face='"
						 + face + "'>" + text + "</FONT>";
			
			if( options.indexOf("b") != -1 ) { s = "<B>" + s + "</B>"; }
			if( options.indexOf("i") != -1 ) { s = "<I>" + s + "</I>"; }
			if( options.indexOf("u") != -1 ) { s = "<U>" + s + "</U>"; }
			
			var align:String = "";
			if( options.indexOf("l") != -1 ) align = "left";
			if( options.indexOf("r") != -1 ) align = "right";
			if( options.indexOf("c") != -1 ) align = "center";
			if( options.indexOf("j") != -1 ) align = "justify";
			
			if( align != "" ) s = "<P align='" + align + "'>" + s + "</P>";
			
			if( href != "" ) s = "<A href='" + href + "'>" + s + "</A>";
			
			return s;
		}
		
		public static function ColFromRGBString(str:String, prefix:String = "#"):uint
		{
			var str:String = "0x" + str.substr(prefix.length);
			
			var col:uint = parseInt(str);
			
			return col;
		}
		
		public static function RGBStringFromCol(col:uint, prefix:String = "#", wantAlpha:Boolean = false):String
		{
			var aa:String = uint((col >> 24) & 0x000000FF).toString(16);
  			var rs:String = uint((col >> 16) & 0x000000FF).toString(16);
  			var gs:String = uint((col >>  8) & 0x000000FF).toString(16);
  			var bs:String = uint((col >>  0) & 0x000000FF).toString(16);
  			
  			if( aa.length == 0 ) aa = "00";
  			if( aa.length == 1 ) aa = 0 + aa;
  			
  			if( rs.length == 0 ) rs = "00";
  			if( rs.length == 1 ) rs = 0 + rs;
  			
  			if( gs.length == 0 ) gs = "00";
  			if( gs.length == 1 ) gs = 0 + gs;
  			
  			if( bs.length == 0 ) bs = "00";
  			if( bs.length == 1 ) bs = 0 + bs;
  			
  			return wantAlpha? prefix + aa + rs + gs + bs
  							: prefix + rs + gs + bs;
  		}
		
		
		
	}
}
